$(function(){
    "use strict";
    // 当点击直接投入的时候
    $('.directInto_box>p').click(function(){
        $(this).siblings('ul.directInto_list').toggleClass('active')
    })
    //当点击核算确定的时候
    $('#check').on('click','.cancel_btn',function(){
        $('#check').removeClass('active')
    })
    // 点击出现搜索框
    $('.hidden_box_showBox').click(function(){
        $(this).siblings('.hidden_box_hiddenBox').toggleClass('active')
        if($(this).children('i').hasClass('icon-jiantou')){
            $(this).children('i').removeClass('icon-jiantou').addClass('icon-jiantou-top')
        }else{
            $(this).children('i').removeClass('icon-jiantou-top').addClass('icon-jiantou')
        }
    })
    // 当确定搜索时
    $('.hiddenBox_list_btn').on('click','button',function(){
        $('.hidden_box_hiddenBox').removeClass('active')
        $('.hidden_box_showBox').children('i').removeClass('icon-jiantou-top').addClass('icon-jiantou')
        if($(this).html() == '确定'){
            $('#dateVal').html(window.query_year_people||'全部')

            let time = $(this).closest('.hidden_box_hiddenBox').find('[data-query="time"]').val()
            let name = $(this).closest('.hidden_box_hiddenBox').find('[data-query="name"]').val()
            window.location.href = `http://jk.com/Admin/Pay/index?time=${time}&staff_name=${name}`

        }else{
            window.query_year_people = null
        }
    })
    //当点击查看记录时
    $('#table_table').on('click','.look_record_btn',function(e){
        e.preventDefault()
        $(this).siblings('.tip_hiddenBox').addClass('active')
    })
    // 当点击工时记录中确认按钮时
    $('#table_table').on('click','.work_record_box>.btn',function(e){
        e.preventDefault()
        $(this).parent().removeClass('active')
    })
    //全选的功能
    $('#checkAll').on('click',function(){
        var checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for(var i=0;i<checkboxs.length;i++){
            var checkbox=checkboxs[i];
            checkbox.checked=this.checked;
        }
    })
    $('#table_table').on('click','input[type=checkbox]:not("#checkAll")',function(){
        "use strict";
        var isCheckedAll = true;
        var checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for (var i = 0; i < checkboxs.length; i++) {
            if (!checkboxs[i].checked) {
                isCheckedAll = false;
                break;
            }
        }
        $('#checkAll')[0].checked = isCheckedAll;
    })

    /*当点击添加按钮时*/
    $('.operation_btn').on('click','a',function(e){
        e.preventDefault()
        if( $(this).html() == '添加' ){
            var add_people = $(this).attr('href')
            $(`[lll=${add_people}]`).addClass('active')
            let htmls = '';
            $.ajax({
                url:'http://jk.com/Admin/Pay/getStaffName',
                type:'get',
                success:function(data){
                    for(var i=0;i<data.length;i++){
                        htmls += `
                            <option value="${data[i].id}" data-dep="${data[i].dep_name}">${data[i].staff_name}</option>
                        `
                    }
                    $(`[lll=${add_people}]`).find('[add_name="add_name"]').html(htmls)
                }
            })
        }
        if( $(this).html() == '核算' ){
            var add_people = $(this).attr('href')
            var checkboxs =$('#table_table input[type=checkbox]:not("#checkAll")');
            var len = 0;
            let hs_arr = [];
            for (var i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    continue
                }else{
                    len++
                }
            }
            if(len == checkboxs.length){
                $('#yz_modal').html('您还没有选择记录！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }else{
                for (let hsi = 0; hsi < checkboxs.length; hsi++) {
                    if (checkboxs[hsi].checked) {
                        hs_arr.push( $(checkboxs[hsi]).closest('tr[data-index]').attr('data-index') )
                    }else{
                        continue;
                    }
                }
                $.ajax({
                    url:'http://jk.com/Admin/ProStaff/calCulStaff',
                    type:'get',
                    data:{
                        id:hs_arr
                    },
                    success:function(data){
                        if(data.code==1){

                        }else{
                            $(add_people).addClass('active')
                        }
                    }
                })
            }


        }
        if( $(this).html() == '批量删除' ){
            var checkboxs =$('input[type=checkbox]:not("#checkAll")');
            var len = 0;
            for (var i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    continue
                }else{
                    len++
                }
            }
            if(len == checkboxs.length){
                $('#yz_modal').html('您还没有选择人员！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }
            let _delete = $(this).attr('href');
            $(_delete).addClass('active')
        }
        if( $(this).html() == '批量导入' ){
            let showbox_id = $(this).attr('href');
            $(showbox_id).addClass('active')
        }
    })
    $(`[lll='add_people']`).find('[add_name="add_name"]').change(function(){
        let dep = $(this).children('option:selected').attr('data-dep')
        $(`[lll='add_people']`).find('[add_name="add_department"]').val(dep)
    })
    // 批量删除
    $('.delete_tip_btn_box').on('click','button',function(){
        "use strict";
        let delete_arr = [];
        if($(this).html() == '确认'){
            let checkboxs =$('#table_table input[type=checkbox]:not("#checkAll")');
            for (let i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    delete_arr.push( $(checkboxs[i]).closest('tr[data-index]').attr('data-index') )
                }else{
                    continue;
                }
            }
            $.ajax({
                url:'http://jk.com/Admin/ProStaff/multiDelStaff',
                type:'post',
                data:{
                    pro_staff_id: delete_arr
                },
                success:function(data){
                    if(data.code==1){
                        for(let r = 0; r < delete_arr.length; r++){
                            $('#table_table').children('tbody').children(`[data-index=${delete_arr[r]}]`).remove();
                        }
                        $('#delete').removeClass('active')
                    }else{
                        $('#yz_modal').html(data.msg)
                        $('#yz_modal').addClass('active')
                        tipShow()
                        return
                    }
                    checkBox()
                }
            })
        }else{
            $('#delete').removeClass('active')
        }
    })
    // 当点击编辑按钮时
    $('#table_table').on('click','.edit_table',function(e){
        e.preventDefault()
        var operation = $(this).attr('href')
        if($(this).html()=='编辑'){
            let operation_id = $(this).closest('tr').attr('data-index');
            let htmls = '';
            $.ajax({
                url:'http://jk.com/Admin/Pay/getStaffName',
                type:'get',
                success:function(data){
                    for(var i=0;i<data.length;i++){
                        htmls += `
                            <option value="${data[i].id}" data-dep="${data[i].dep_name}">${data[i].staff_name}</option>
                        `
                    }
                    $(`[lll=${operation}]`).find('[add_name="edit_name"]').html(htmls)
                }
            })
            $.ajax({
                url:'http://jk.com/Admin/ProStaff/editStaff',
                type:'get',
                data:{
                    id:operation_id
                },
                success:function(data){
                    console.log(data)
                    if(data.code==1){
                        let datas = data.data;
                        $(`[lll=${operation}]`).find(`[add_name='start_time_edit']`).val(datas.start_time)
                        $(`[lll=${operation}]`).find(`[add_name='end_time_edit']`).val(datas.end_time)
                        $(`[lll=${operation}]`).find(`[add_name='edit_name']`).val(datas.staff_id)
                        $(`[lll=${operation}]`).find(`[add_name='edit_department']`).val(datas.dep_name)
                        $(`[lll=${operation}]`).find(`[add_name='edit_workTime']`).val(datas.work_hours)
                        $(`[lll=${operation}]`).find(`[add_name='edit_workTimeRecord']`).val(datas.work_record)
                        window.sessionStorage['operation_id']=operation_id
                        $(`[lll=${operation}]`).addClass('active');
                    }
                }
            })
        }else{//当他等于取消核算的时候
            $.ajax({

            })
        }

    })
    $(`[lll='edit_people']`).find('[add_name="edit_name"]').change(function(){
        let dep = $(this).children('option:selected').attr('data-dep')
        $(`[lll='edit_people']`).find('[add_name="edit_department"]').val(dep)
    })

    // 确认按钮点击时
    $('#add_btn').click(function(){
        var start_time_add = $(this).closest('[lll]').find('[add_name=start_time_add]').val()
        var end_time_add = $(this).closest('[lll]').find('[add_name=end_time_add]').val()

        var add_name = $(this).closest('[lll]').find('[add_name=add_name] option:selected').text()
        var add_name_id = $(this).closest('[lll]').find('[add_name=add_name]').val()
        var add_department = $(this).closest('[lll]').find('[add_name=add_department]').val()
        var add_workTime = $(this).closest('[lll]').find('[add_name=add_workTime]').val()
        var add_workTimeRecord = $(this).closest('[lll]').find('[add_name=add_workTimeRecord]').val()

        var start_timeList = ($(this).closest('[lll]').find('[add_name=start_time_add]').val()).split('-')
        var end_time_addList = ($(this).closest('[lll]').find('[add_name=end_time_add]').val()).split('-')
        var start_time_add_month = start_timeList[1]
        var end_time_add_month = end_time_addList[1]
        var start_time_add_date = start_timeList[2]
        var end_time_add_date = end_time_addList[2]

        if(start_time_add_month != end_time_add_month){
            $('#yz_modal').html('开始和结束时间不能跨月！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }else if(end_time_add_date <= start_time_add_date){
            $('#yz_modal').html('结束日期不得小于或等于开始日期！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }
        if(!add_workTimeRecord){
            add_workTimeRecord = '暂无'
        }
        if(start_time_add == '' || end_time_add == '' || add_name == '' || add_department == '' || add_workTime == ''){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }

        let that = this
        $.ajax({
            url:'http://jk.com/Admin/ProStaff/addStaff',
            type:'post',
            data:{
                start_time: start_time_add,
                end_time: end_time_add,
                staff_id: add_name_id,
                work_hours: add_workTime,
                work_record:add_workTimeRecord
            },
            success:function(data){
                console.log(data)
                if(data.code==1){
                    var peopleTableHTML = $('#table_table').children('tbody').html()
                    peopleTableHTML += `
                            <tr data-index="${data.id}">
                                    <td><input type="checkbox" class="checkboxStyle"></td>
                                    <td data-name="start_time_edit">${start_time_add}</td>
                                    <td data-name="end_time_edit">${end_time_add}</td>
                                    <td data-name="edit_name">${add_name}</td>
                                    <td data-name="edit_department">${add_department}</td>
                                    <td data-name="edit_workTime">${add_workTime}</td>
                                    <td class="look_record" data-name="edit_workTimeRecord">
                                        <a href="#" class="look_record_btn">查看记录</a>
                                        <div class="tip_hiddenBox work_record_box">
                                            <span class="work_record_title">工时记录</span>
                                            <p class="work_record_main">${add_workTimeRecord}</p>
                                            <button class="btn btn-sm btn-success">确认</button>
                                        </div>
                                    </td>
                                    <td style><a href="edit_people" class="edit_table">编辑</a></td>
                                </tr>
                    `
                    $('#table_table').children('tbody').html(peopleTableHTML)

                    $(that).closest('[lll]').find('[add_name]').val('')
                    $(that).closest('[lll]').removeClass('active')
                }else{
                    $('#yz_modal').html(data.msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                }
            }
        })

    })
    // 确认并新增按钮点击时
    $('#add_AddNew_btn').click(function(){
        var start_time_add = $(this).closest('[lll]').find('[add_name=start_time_add]').val()
        var end_time_add = $(this).closest('[lll]').find('[add_name=end_time_add]').val()

        var add_name = $(this).closest('[lll]').find('[add_name=add_name] option:selected').text()
        var add_name_id = $(this).closest('[lll]').find('[add_name=add_name]').val()
        var add_department = $(this).closest('[lll]').find('[add_name=add_department]').val()
        var add_workTime = $(this).closest('[lll]').find('[add_name=add_workTime]').val()
        var add_workTimeRecord = $(this).closest('[lll]').find('[add_name=add_workTimeRecord]').val()

        var start_timeList = ($(this).closest('[lll]').find('[add_name=start_time_add]').val()).split('-')
        var end_time_addList = ($(this).closest('[lll]').find('[add_name=end_time_add]').val()).split('-')
        var start_time_add_month = start_timeList[1]
        var end_time_add_month = end_time_addList[1]
        var start_time_add_date = start_timeList[2]
        var end_time_add_date = end_time_addList[2]

        if(start_time_add_month != end_time_add_month){
            $('#yz_modal').html('开始和结束时间不能跨月！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }else if(end_time_add_date <= start_time_add_date){
            $('#yz_modal').html('结束日期不得小于或等于开始日期！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }
        if(!add_workTimeRecord){
            add_workTimeRecord = '暂无'
        }
        if(start_time_add == '' || end_time_add == '' || add_name == '' || add_department == '' || add_workTime == ''){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }

        let that = this
        $.ajax({
            url:'http://jk.com/Admin/ProStaff/addStaff',
            type:'post',
            data:{
                start_time: start_time_add,
                end_time: end_time_add,
                staff_id: add_name_id,
                work_hours: add_workTime,
                work_record:add_workTimeRecord
            },
            success:function(data){
                if(data.code==1){
                    var peopleTableHTML = $('#table_table').children('tbody').html()
                    peopleTableHTML += `
                            <tr data-index="${data.id}">
                                    <td><input type="checkbox" class="checkboxStyle"></td>
                                    <td data-name="start_time_edit">${start_time_add}</td>
                                    <td data-name="end_time_edit">${end_time_add}</td>
                                    <td data-name="edit_name">${add_name}</td>
                                    <td data-name="edit_department">${add_department}</td>
                                    <td data-name="edit_workTime">${add_workTime}</td>
                                    <td class="look_record" data-name="edit_workTimeRecord">
                                        <a href="#" class="look_record_btn">查看记录</a>
                                        <div class="tip_hiddenBox work_record_box">
                                            <span class="work_record_title">工时记录</span>
                                            <p class="work_record_main">${add_workTimeRecord}</p>
                                            <button class="btn btn-sm btn-success">确认</button>
                                        </div>
                                    </td>
                                    <td style><a href="edit_people" class="edit_table">编辑</a></td>
                                </tr>
                    `
                    $('#table_table').children('tbody').html(peopleTableHTML)

                    $(that).closest('[lll]').find('[add_name]').val('')
                    // $(that).closest('[lll]').removeClass('active')
                }else{
                    $('#yz_modal').html(data.msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                }
            }
        })
    })
    // 保存按钮点击时
    $('#save_btn').click(function(){
        let start_time_edit = $(this).closest('[lll]').find('[add_name=start_time_edit]').val()
        let end_time_edit = $(this).closest('[lll]').find('[add_name=end_time_edit]').val()

        let edit_name = $(this).closest('[lll]').find('[add_name=edit_name] option:selected').text()
        let edit_name_id = $(this).closest('[lll]').find('[add_name=edit_name]').val()
        let edit_department = $(this).closest('[lll]').find('[add_name=edit_department]').val()
        let edit_workTime = $(this).closest('[lll]').find('[add_name=edit_workTime]').val()
        let edit_workTimeRecord = $(this).closest('[lll]').find('[add_name=edit_workTimeRecord]').val()

        var start_timeList = start_time_edit.split('-')
        var end_time_addList = end_time_edit.split('-')
        var start_time_edit_month = start_timeList[1]
        var end_time_edit_month = end_time_addList[1]
        var start_time_edit_date = start_timeList[2]
        var end_time_edit_date = end_time_addList[2]

        if(start_time_edit_month != end_time_edit_month){
            $('#yz_modal').html('开始和结束时间不能跨月！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }else if(end_time_edit_date <= start_time_edit_date){
            $('#yz_modal').html('结束日期不得小于或等于开始日期！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }
        if(start_time_edit == '' || end_time_edit == '' || edit_name == '' || edit_department == '' || edit_workTime == ''){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }

        let operation_id = window.sessionStorage['operation_id']
        let that = this
        $.ajax({
            url:'http://jk.com/Admin/ProStaff/editStaff',
            type:'post',
            data:{
                id:operation_id,
                start_time: start_time_edit,
                end_time: end_time_edit,
                staff_id: edit_name_id,
                work_hours: edit_workTime,
                work_record:edit_workTimeRecord
            },
            success:function(data){
                console.log(data)
                if(data.code==1){
                    // var tableHTML = $('#table_table').children('tbody').find(`[data-index=${operation_id}]`)
                    // tableHTML.children('[data-name=start_time_edit]').html(start_time_edit)
                    // tableHTML.children('[data-name=end_time_edit]').html(end_time_edit)
                    // tableHTML.children('[data-name=edit_name]').html(edit_name)
                    // tableHTML.children('[data-name=edit_department]').html(edit_department)
                    // tableHTML.children('[data-name=edit_workTime]').html(edit_workTime)
                    // tableHTML.children('[data-name=edit_workTimeRecord]').html(edit_workTimeRecord)
                    var HTML = ''
                    HTML = `
                                    <td><input type="checkbox" class="checkboxStyle"></td>
                                    <td data-name="start_time_edit">${start_time_edit}</td>
                                    <td data-name="end_time_edit">${end_time_edit}</td>
                                    <td data-name="edit_name">${edit_name}</td>
                                    <td data-name="edit_department">${edit_department}</td>
                                    <td data-name="edit_workTime">${edit_workTime}</td>
                                    <td class="look_record" data-name="edit_workTimeRecord">
                                        <a href="#" class="look_record_btn">查看记录</a>
                                        <div class="tip_hiddenBox work_record_box">
                                            <span class="work_record_title">工时记录</span>
                                            <p class="work_record_main">${edit_workTimeRecord}</p>
                                            <button class="btn btn-sm btn-success">确认</button>
                                        </div>
                                    </td>
                                    <td style><a href="edit_people" class="edit_table">编辑</a></td>
                    `
                    $('#table_table').children('tbody').find(`tr[data-index=${operation_id}]`).html(HTML)
                    $(that).parent().parent().removeClass('active')
                }
            }
        })


    })
    // 取消按钮点击时
    $('.button_list').on('click','button',function(){
        if($(this).hasClass('cancel_btn')){
            $(this).closest('[lll]').removeClass('active')
        }
    })

    // 上传文件
    $('#file_upload').change(function (e) {
        $(this).siblings('.import_tip_btn_box').find('button').removeClass('btn-grey').addClass('btn-success')
    })
    $('#import').find('button').click(function(e){
        "use strict";
        e.preventDefault()
        if($(this).hasClass('btn-success')){
            if($(this).html()=='确定导入'){
                var formData = new FormData($("#file_form")[0]);
                $.ajax({
                    async : false,
                    cache : false,
                    type : "post",
                    data : formData,
                    url : 'http://jk.com/Admin/ProStaff/importStaff',
                    dataType : 'json',
                    contentType: false, //必须
                    processData: false, //必须
                    success : function(data) {
                        if(data.code==1){
                            $('#import').removeClass('active')
                        }else{
                            $('#yz_modal').html(data.msg)
                            $('#yz_modal').addClass('active')
                            tipShow()
                        }
                    }
                })
            }
        }

    })

    // 弹出来的小框框
    function tipShow(){
        window.tipBox = setTimeout(function(){
            // $('#yz_modal').removeClass('active')
            // $('#yz_modal').addClass('fadeOut')
            $('#yz_modal').removeClass('active')
            clearTimeout(tipBox)
        },2000)
    }
    //关闭所有已选择的框框
    function checkBox(){
        var checkboxs =$('input[type=checkbox]');
        for (var i = 0; i < checkboxs.length; i++) {
            if (!checkboxs[i].checked) {
                continue;
            }else{
                $(checkboxs[i]).attr('checked',false)
            }
        }
    }

    //当点击区域不在导入弹框中时则关闭弹框
    $(document).click(function(e){
        let $this = $(e.target);
        if($this.closest('#import').length==0){
            if($this.attr('href') != '#import' && $this.children().attr('href') != '#import'){
                $('#import').removeClass('active');
            }
        }
    })


})